/*
     ui_typw==21 Func文本输入框
 */
(function ($) {
    $.fn.yqTextFunc = function (opts, param) {
        if (typeof opts === 'string') {
            return $.fn.yqTextFunc.methods[opts](this, param);
        }

        const $this = $(this);
        let opt;
        
        if ($this.data("yqtype") === "yqTextFunc") {
            opt = $.extend({}, options($this), opts);
        } else {
            opt = $.extend({}, $.fn.yqTextFunc.defaults, opts);
            let uniqueId = $this.attr("ID");
            if (typeof uniqueId === 'undefined') {
                uniqueId = $.getUniqueID();
                $this.attr("ID", uniqueId);
            }
        }

        // 处理宽度设置
        if (typeof opt.width === 'undefined' && $this.width() > 0) {
            opt.width = $this.width();
        } else if (typeof opt.width !== 'undefined') {
            $this.width(opt.width);
        }

        //变量信息
        $this.data("textfunc_opt", opt)
             .data("yqtype", "yqTextFunc");

        init_text_before($this);
    };

    $.fn.yqTextFunc.defaults = {
        value: "", //默认值
        isEdit: true,
        beforeBindingTitle: function (callback) {
            callback();
        }, //标题绑定前
        afterBindingTitle: function () { },   //标题绑定后

        funcCallback: function (prevent_e, mdiinfo, callback) {
            var value = "12-12-12-12";
            callback(true, value);
        },
        beforeClear: function (callback) {
            callback(true);
        },
        afterChange: function (value) {

        },
    };

    $.fn.yqTextFunc.methods = {
        options: function (jq) {
            return options(jq);
        },
        destroy: function (jq) {
            destroy(jq);
        },
        resize: function (jq, width) {
            resize(jq, width);
        },
        clear: function (jq, data) {
            var opt = options(jq);
            opt.afterChange.call(jq, null);
            setValue($(jq[0]), "");
        },
        setValue: function (jq, value) {
            setValue($(jq[0]), value);
        },
        getValue: function (jq) {
            return getValue($(jq[0]));
        },
        isEdit: function (jq, tf) {
            isEdit($(jq[0]), tf);
        }
    };

    function options($object) {
        return $object.data("textfunc_opt");
    }

    function destroy($object) {
        $object.textbox("destroy");
    }

    function resize($object, width) {
        $object.textbox("resize", width);
    }

    function init_text_before($object) {
        var opt = options($object);
        opt.beforeBindingTitle.call($object,
            function () {
                init_text($object);
            });
    }

    function init_text($object) {
        const opt = options($object);

        const text_opt = {
            editable: false,
            iconAlign: 'right',
            icons: [{
                iconCls: 'yqwin-combo-btn-icon fa fa-pencil',
                handler: function (e) {
                    const pp = $object.textbox("textbox");
                    const preventE = $.getPreventDefault(pp[0]);
                    preventE.id = $object.attr("id");
                    preventE.options = opt;

                    const mdiinfo = $.yqGetWindParams();

                    opt.funcCallback.call($object, preventE, mdiinfo,
                        function (tf, value) {
                            if (!tf) return;

                            if (typeof opt.p_target !== "undefined") {
                                const ed = $.yqTextEditor(opt.p_target, opt.p_field);
                                if (ed !== null) {
                                    const $target = $(ed.target);
                                    $target.yqTextFunc('setValue', value);
                                    opt.afterChange.call($target, value);
                                }
                            } else {
                                opt.value = value;
                                $object.textbox("setText", value);
                                opt.afterChange.call($object, value);
                            }
                        });
                }
            }, {
                iconCls: 'yqwin-combo-btn-icon fa fa-times',
                handler: function (e) {
                    opt.beforeClear.call($object, function (tf) {
                        if (!tf) return;
                        
                        opt.afterChange.call($object, null);
                        setValue($object, "");
                    });
                }
            }]
        };

        $object.textbox(text_opt);
        setValue($object, opt.value);
        isEdit($object, opt.isEdit);
        opt.afterBindingTitle.call($object);
    }

    function setValue($object, value) {
        const opt = options($object);
        opt.value = value ?? "";
        $object.textbox("setText", opt.value);
    }

    function getValue($object) {
        const opt = options($object);
        return opt.value || null;
    }

    function isEdit($object, tf) {
        const opt = options($object);
        const icon0 = $object.textbox("getIcon", 0);
        const icon1 = $object.textbox("getIcon", 1);
        const textbox = $object.textbox("textbox");

        opt.isEdit = tf;
        if (opt.isEdit) {
            icon0.show();
            icon1.show();
            textbox.width(opt.width - 52 - 3);
        } else {
            icon0.hide();
            icon1.hide();
            textbox.width(opt.width - 3);
        }
    }
})(jQuery);
